<template>
  <div class="com-container">
    <div class="title" :style="comStyle">
      <span>{{ "▎" + showTitle }}</span>
      <span
        class="iconfont title-icon"
        :style="comStyle"
        @click="showChoice = !showChoice"
        >&#xe6eb;</span
      >
      <div class="select-con" v-show="showChoice" :style="marginStyle">
        <div
          class="select-item"
          v-for="item in selecteTypes"
          :key="item.key"
          @click="handleSelect(item.key)"
        >
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class="com-echart" ref="trend_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { getThemeValue } from '@/utils/theme_utils';
export default {
  data() {
    return {
      echartInstance: null,
      // 服务器返回的数据
      allData: null,
      //   是否显示可选项
      showChoice: false,
      //   显示的数据类型
      choiceType: "map",
      //   指明标题的字体大小
      titleFontSize: 0
    };
  },
  computed: {
    selecteTypes() {
      return this.allData
        ? this.allData.type.filter(v => v.key !== this.choiceType)
        : [];
    },
    showTitle() {
      return this.allData ? this.allData[this.choiceType].title : "";
    },
    // 标题大小样式
    comStyle() {
      return {
        fontSize: this.titleFontSize + "px",
        color: getThemeValue(this.theme).titleColor
      };
    },
    marginStyle() {
      return {
        marginLeft: this.titleFontSize + "px"
      };
    },
    ...mapState(['theme']),
  },
  watch:{
    theme(){
      // 销毁当前图表
      this.echartInstance.dispose();
      this.initChart();
      this.screenAdapter();
      this.updateChart();
    }
  },
  created(){
    //这里通过这个方法提前将要执行的函数给到工具类中Mapping
    this.$socket.registerCallBack('trendData', this.getData)
  },
  mounted() {
    this.initChart();
    // this.getData();
    // 发送数据给服务器,通知服务器给数据
    this.$socket.send({
      action: 'getData',
      socketType: 'trendData',
      chartName: 'trend',
      value: ''
    });
    window.addEventListener("resize", this.screenAdapter);
    // 首次适配
    this.screenAdapter();
  },
  beforeDestroy() {
    // 移除resize事件
    window.removeEventListener("resize", this.screenAdapter);
    // 组件卸载时，取消数据的保留
    this.$socket.unRegisterCallBack('trendData');
  },
  methods: {
    initChart() {
      this.echartInstance = this.$echarts.init(this.$refs.trend_ref, this.theme);
      const initOption = {
        grid: {
          left: "3%",
          top: "35%",
          right: "4%",
          bottom: "1%",
          containLabel: true
        },
        tooltip: {
          trigger: "axis"
        },
        legend: {
          left: 20,
          top: "15%",
          icon: "circle"
        },
        xAxis: {
          type: "category",
          // 图表类目紧挨边缘
          boundaryGap: false
        },
        yAxis: {
          type: "value"
        }
      };
      this.echartInstance.setOption(initOption);
    },
    // ret 服务端发送给客户端的图标数据
    getData(ret) {
      // const { data: ret } = await this.$http.get("trend");
      this.allData = ret;
      this.updateChart();
    },
    updateChart() {
      // 半透明的颜色值
      const colorArr1 = [
        "rgba(11, 168, 44, 0.5)",
        "rgba(44, 110, 255, 0.5)",
        "rgba(22, 242, 217, 0.5)",
        "rgba(254, 33, 30, 0.5)",
        "rgba(250, 105, 0, 0.5)"
      ];
      // 全透明的颜色值
      const colorArr2 = [
        "rgba(11, 168, 44, 0)",
        "rgba(44, 110, 255, 0)",
        "rgba(22, 242, 217, 0)",
        "rgba(254, 33, 30, 0)",
        "rgba(250, 105, 0, 0)"
      ];
      // 处理数据
      // 类目轴的数据
      const timeArr = this.allData.common.month;
      // y轴的数据
      const valueArr = this.allData[this.choiceType].data;
      const seriesArr = valueArr.map((v, index) => {
        return {
          name: v.name,
          type: "line",
          // 堆叠图
          stack: this.choiceType,
          data: v.data,
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colorArr1[index]
              },
              {
                offset: 1,
                color: colorArr2[index]
              }
            ])
          }
        };
      });
      // 图例的数据
      const legendArr = valueArr.map(v => v.name);

      const dataOption = {
        xAxis: {
          data: timeArr
        },
        legend: {
          data: legendArr
        },
        series: seriesArr
      };
      this.echartInstance.setOption(dataOption);
    },
    screenAdapter() {
      this.titleFontSize = (this.$refs.trend_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2
          }
        }
      };
      this.echartInstance.setOption(adapterOption);
      // 手动调用图表对象的resize以适配
      this.echartInstance.resize();
    },
    handleSelect(currentType) {
      this.choiceType = currentType;
      this.updateChart();
      this.showChoice = false;
    }
  }
};
</script>
<style lang="less" scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: white;
  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }
  .select-con {
    background: #222733;
  }
}
</style>
